package top.forlenet.service.impl;


import top.forlenet.dao.UserDao;
import top.forlenet.dao.impl.UserDaoImpl;
import top.forlenet.entity.User;
import top.forlenet.param.RegisterParam;
import top.forlenet.service.UserService;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    @Override
    public User login(String phone, String password) {
        User user = userDao.selectByPhone(phone);
        if (user == null) {
            throw new RuntimeException("手机号不存在");
        }
        if (!user.getPassword().equals(password)) {
            throw new RuntimeException("密码错误");
        }

        return user;
    }

    @Override
    public void register(RegisterParam param) {

        if (!param.getPassword().equals(param.getRePassword())) {
            throw new RuntimeException("两次输入的密码不一致");
        }

        User temp = userDao.selectByPhone(param.getPhone());
        if (temp != null) {
            throw new RuntimeException("该手机号已注册");
        }
        User user = new User();
        user.setPhone(param.getPhone());
        user.setPassword(param.getPassword());
        user.setPid(param.getPid());
        userDao.insert(user);

    }
}
